package com.xinliu.fastchat.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.xinliu.fastchat.constant.FriendRequestConstant;
import com.xinliu.fastchat.context.BaseContext;
import com.xinliu.fastchat.domain.core.R;
import com.xinliu.fastchat.domain.dto.FriendRequestDto;
import com.xinliu.fastchat.domain.po.Friend;
import com.xinliu.fastchat.domain.po.FriendRequest;
import com.xinliu.fastchat.domain.vo.FriendRequestVo;
import com.xinliu.fastchat.mapper.FcFriendRequestMapper;
import com.xinliu.fastchat.service.IFriendRequestService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xinliu.fastchat.service.IFriendService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 心流
 * @since 2025-10-01
 */
@Service
@RequiredArgsConstructor
public class FriendRequestServiceImpl extends ServiceImpl<FcFriendRequestMapper, FriendRequest> implements IFriendRequestService {

    private final IFriendService iFriendService;

    @Override
    public R request(FriendRequestDto friendRequestDto) {
        Integer userId = BaseContext.getId();
        //校验
        if (userId == friendRequestDto.getReceiveId()) {
            return R.error("现实中你是否认识自己?");
        }

        //查询是否已申请过辽
        if (baseMapper.exists(new LambdaQueryWrapper<FriendRequest>()
                .eq(FriendRequest::getSendId, BaseContext.getId())
                .eq(FriendRequest::getReceiveId, friendRequestDto.getReceiveId()))) {
            return R.error("已申请过了哟, 再申请我就不客气了!ovo");
        }

        //查询双方好友关系
        if (iFriendService.getBaseMapper().exists(new LambdaQueryWrapper<Friend>()
                .eq(Friend::getUserId, userId)
                .eq(Friend::getFriendId, friendRequestDto.getReceiveId()))) {
            return R.error("你们已经是好友关系了捏");
        }

        //保存请求
        FriendRequest friendRequest = new FriendRequest();
        BeanUtils.copyProperties(friendRequestDto, friendRequest);
        friendRequest.setSendId(userId);
        friendRequest.setStatus(FriendRequestConstant.WAIT);
        friendRequest.setIsDeleted(0);
        save(friendRequest);
        return R.ok("申请成功");
    }

    @Override
    public R handleRequest(Integer friendRequestId, Integer status) {
        Integer userId = BaseContext.getId();

        FriendRequest friendRequest = baseMapper.selectOne(new LambdaQueryWrapper<FriendRequest>()
                .eq(FriendRequest::getId, friendRequestId)
                .eq(FriendRequest::getReceiveId, userId));

        //校验该请求归属
        if (friendRequest == null) {
            return R.error("这条请求不属于你, 你是从哪搞来的?");
        }

        if (friendRequest.getStatus() != FriendRequestConstant.WAIT) {
            return R.error("这条请求已经给你处理过了, 你在瞎捣鼓这些干嘛呢?");
        }

        //设置状态
        switch (status) {
            case FriendRequestConstant.AGREE:
                createFriendRelate(friendRequest.getSendId(), friendRequest.getReceiveId());
                createFriendRelate(friendRequest.getReceiveId(), friendRequest.getSendId());
            case FriendRequestConstant.REJECT:
                friendRequest.setStatus(status);
                baseMapper.update(friendRequest, new LambdaUpdateWrapper<FriendRequest>()
                        .eq(FriendRequest::getId, friendRequest.getId()));
                break;
            default:
                return R.error("无法处理请求,参数错误!!!");
        }

        return R.ok("已同意");
    }

    @Override
    public List<FriendRequestVo> requestList(Integer id) {
        return baseMapper.requestList(id);
    }

    private void createFriendRelate(Integer userId, Integer friendId) {
        Friend friend = new Friend();
        friend.setUserId(userId);
        friend.setFriendId(friendId);
        friend.setStatus(0);
        friend.setIsDeleted(0);
        iFriendService.save(friend);
    }
}
